#include <string.h>
#include <iostream>
using std::cout;
using std::endl;

class Computer
{
public:
    Computer(const char * brand,double price)
    : _brand(new char[strlen(brand) + 1]())
    , _price(price)
    {
        strcpy(_brand,brand);
        cout << "Computer(const char *,double)" << endl;
    }


    ~Computer(){
        cout << "~Computer()" << endl;
        if(_brand){
            delete [] _brand;
            _brand = nullptr;
        }
    }

    Computer(const Computer & rhs)
    : _brand(new char[strlen(rhs._brand) + 1]())
    , _price(rhs._price)
    {
        strcpy(_brand,rhs._brand);
        cout << "拷贝构造" << endl;
    }

    Computer & operator=(const Computer & rhs){
        cout << "赋值运算符函数" << endl;
        if(this != &rhs){
            delete [] _brand;
            _brand = new char[strlen(rhs._brand) + 1]();
            strcpy(_brand,rhs._brand);
            _price = rhs._price;
        }
        return *this;
    }
    
    //const Computer * const this;
    void print() const{
        /* _brand[0] = 'X';//需要人为避免 */

        /* _brand = new char[10](); */
        /* _price = 1; */
        cout << "brand:" << _brand << endl;
        cout << "price:" << _price << endl;
    }
private:
    /* const char * _brand; */
    char * _brand;
    double _price;
};

void test0(){
    Computer pc("Apple",20000);
    pc.print();
}

int main(void){
    test0();
    return 0;
}
